<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@taglib uri="http://java.fckeditor.net" prefix="FCK" %>

<div>
    <logic:present name="acc">
        <p align="right">
            <a href="AdminTour.do?action=listTour" >Tour manager</a>&emsp;&emsp;&emsp;
            <a href="AdminTour.do?action=listTourServices" >Tour Service manager</a> &emsp;&emsp;&emsp;
            <a href="AdminTour.do?action=listTourType" >Tour Type manager</a> &emsp;&emsp;&emsp;
            <a href="AdminTour.do?action=listTourLocation" >Location manager</a>
        </p>
        <form action="AdminTour.do?action=UpdateTour" method="post" name="frTour">
            <table width="100%" border="0">
                <tr>
                    <td><font size="2">Tour id :</font></td>
                    <td><input type="text" readonly="true" name="tourId" value="${tourDTO.tourId}"></td>
                </tr>
                <tr>
                    <td><font size="2">Tour Name :</font></td>
                    <td><input type="text" size="80" name="tourName" value="${tourDTO.tourName}"></td>
                </tr>
                <tr>
                    <td><font size="2">Introduction :</font></td>
                    <td>
                        <FCK:editor instanceName="intro" value="${tourDTO.introduction}" width="100%" height="200">
                        </FCK:editor>
                    </td>
                </tr>
                <tr>
                    <td><font size="2">Type Name :</font> </td>

                    <td>
                        <fieldset  style="color:black;"><legend><b><font size="2">Tour Type</font></b></legend>
                            <font size="2">Type Name :</font> <select name="type">
                                <option value="${tourDTO.tourTypeDTO.tourTypeId}">
                                    ${tourDTO.tourTypeDTO.tourTypeName}
                                </option>
                                <logic:present name="listTourType">
                                    <logic:iterate name="listTourType" id="listTourType">
                                        <logic:notEqual name="listTourType" property="tourTypeId" value="${tourDTO.tourTypeDTO.tourTypeId}">
                                            <option value="<bean:write name="listTourType" property="tourTypeId"/>">
                                                <bean:write name="listTourType" property="tourTypeName"/>
                                            </option>
                                        </logic:notEqual>
                                    </logic:iterate>
                                </logic:present>
                            </select><br>
                            <font size="2">Departure  :</font> <input type="text" name="Departure" value="${tourDTO.departure}"><br>
                            <font size="2">Destination :</font>
                            <input type="text" name="Destination" value="${tourDTO.destination}"><br>
                            <font size="2">Duration :</font>
                            <input type="text" size="2" name="Duration" value="${tourDTO.duration}">
                        </fieldset>
                    </td>

                </tr>


                <tr>
                    <td><font size="2">Tour Price :</font> </td>

                    <td>
                        <fieldset  style="color:black;"><legend><b><font size="2">Tour Type</font></b></legend>
                            <table width="100%" border="0">
                                <tr>
                                    <td><font size="2">1 px</font> </td>
                                    <td><font size="2">2-3 px</font> </td>
                                    <td><font size="2">4-6 px</font> </td>
                                    <td><font size="2">7 px </font></td>
                                </tr>
                                <tr>
                                    <logic:iterate name="listPrice" id="listPrice">
                                        <td>
                                            <input type="hidden" name="priceId<bean:write name="listPrice" property="quantity"/>" value="<bean:write name="listPrice" property="tourPriceId"/>">
                                            <input type="text" name="<bean:write name="listPrice" property="quantity"/>per" value="<bean:write name="listPrice" property="price"/>">
                                        </td>
                                    </logic:iterate>
                                </tr>
                            </table>
                        </fieldset>
                    </td>

                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <input type="submit" value="Update" name="action"/>
                    </td>
                </tr>
            </table>

        </form>
        <hr>
        <fieldset  style="color:black;"><legend><b><font size="2">Schedules</font></b></legend>
            <table width="700" border="0">
                <tr>
                    <td width="50%"><font size="2">Schedules</font></td>
                </tr>
                <tr>
                    <td width="45%" id="frSchedule">
                        <form name="frSchedule" action="AdminTour.do?action=UpdateSchedule" method="post">
                            <table width="100%" border="0">
                                <tr>
                                    <td>
                                        <font size="2">Location</font><input type="hidden" name="tourId" value="${tourDTO.tourId}">
                                    </td>
                                    <td>
                                        <font size="2">Description</font>
                                    </td>
                                    <td>
                                        <img title="Add" src="admin/img/action_add.png" onclick="loadXMLDoc('newSchedule','AdminTour.do?action=newSchedule&tourId=${tourDTO.tourId}')"  style="cursor:pointer">
                                    </td>
                                </tr>
                                <logic:present name="listSchedules">
                                    <logic:iterate name="listSchedules" id="listSchedules">
                                        <tr>
                                            <td>
                                                <select name="localtion<bean:write name="listSchedules" property="schedulesId"/>">
                                                    <option value="<bean:write name="listSchedules" property="locationId"/>">
                                                        <bean:write name="listSchedules" property="locationName"/>
                                                    </option>
                                                    <logic:iterate name="listLocaltion" id="listLocal">
                                                        <logic:notEqual name="listLocal" property="locationId" value="${listSchedules.locationId}">
                                                            <option value="<bean:write name="listLocal" property="locationId"/>">
                                                                <bean:write name="listLocal" property="locationName"/>
                                                            </option>
                                                        </logic:notEqual>
                                                    </logic:iterate>
                                                </select>
                                            </td>
                                            <td>
                                                <textarea name="ScheDes<bean:write name="listSchedules" property="schedulesId"/>" cols="70" rows="5"><bean:write name="listSchedules" property="description"/></textarea>
                                            </td>
                                            <td>
                                                <img title="Add" src="admin/img/action_add.png" onclick="loadXMLDoc('newSchedule','AdminTour.do?action=newSchedule&tourId=${tourDTO.tourId}')"  style="cursor:pointer">
                                                <a href="AdminTour.do?action=deleteSchedule&tourId=${tourDTO.tourId}&scheduleId=<bean:write name="listSchedules" property="schedulesId"/>">
                                                    <img title="Delete" src="admin/img/action_delete.png" style="cursor:pointer">
                                                </a>
                                            </td>
                                        </tr>
                                    </logic:iterate>
                                    <tr>
                                        <td colspan="3"><input type="submit" name="action" value="Update"></td>
                                    </tr>
                                </logic:present>
                            </table>
                        </form>
                        <div id="newSchedule"></div>
                    </td>
                    <td valign="top">

                        <div id="newSer"></div>
                    </td>
                </tr>
            </table>
        </fieldset>
        <form name="frSer" action="AdminTour.do?action=UpdateSevice" method="post">
            <fieldset  style="color:black;"><legend><b><font size="2">Tour Service</font></b></legend>
                <table width="100%" border="0">
                    <tr>
                        <td colspan="4"><font size="2">Tour Service </font><input type="hidden" name="tourId" value="${tourDTO.tourId}"> </td>
                    </tr>
                    <tr>
                        <td><font size="2">Service</font></td>
                        <td><font size="2">Detail</font></td>
                        <td><font size="2">Price</font></td>
                        <td><font size="2">Action</font></td>
                    </tr>
                    <logic:present name="listService">
                        <logic:iterate name="listService" id="listService">
                            <tr>
                                <td>
                                    <select name="ser<bean:write name="listService" property="tourDetailDTO.tourDetailId"/>">
                                        <option value="<bean:write name="listService" property="tourServiceId"/>"><bean:write name="listService" property="tourServiceName"/></option>
                                        <logic:present name="listAllService">
                                            <logic:iterate name="listAllService" id="listAllSer">
                                                <logic:notEqual name="listAllSer" property="tourServiceId" value="${listService.tourServiceId}">
                                                    <option value="<bean:write name="listAllSer" property="tourServiceId"/>"><bean:write name="listAllSer" property="tourServiceName"/></option>
                                                </logic:notEqual>
                                            </logic:iterate>
                                        </logic:present>
                                    </select>
                                </td>
                                <td>
                                    <textarea name="detail<bean:write name="listService" property="tourDetailDTO.tourDetailId"/>" cols="35" rows="5" ><bean:write name="listService" property="tourDetailDTO.detail"/></textarea>
                                </td>
                                <td><input  type="text" name="price<bean:write name="listService" property="tourDetailDTO.tourDetailId"/>" size="2" value="<bean:write name="listService" property="tourDetailDTO.price"/>"/></td>
                                <td>
                                    <img title="Add" src="admin/img/action_add.png" onclick="loadXMLDoc('newSer','AdminTour.do?action=listServiceTour&tourId=${tourDTO.tourId}')"  style="cursor:pointer">
                                    <a href="AdminTour.do?action=deleteService&tourId=${tourDTO.tourId}&detailId=<bean:write name="listService" property="tourDetailDTO.tourDetailId"/>">
                                        <img title="Delete" src="admin/img/action_delete.png" style="cursor:pointer">
                                    </a>
                                </td>
                            </tr>

                        </logic:iterate>
                        <tr>
                            <td colspan="4"><input type="submit" value="Update" name="action"></td>
                        </tr>
                    </logic:present>
                </table>
            </fieldset>
        </form>
    </logic:present>
    <logic:notPresent name="acc">
        <jsp:forward page="adminHome.jsp"/>
    </logic:notPresent>
</div>
<script>
    function addnew(){
        document.getElementById("image").innerHTML += "<input name='images' type='file' id='img'> <br/>";
    }
    //check image
    function checkimg(element,helperMsg){
        if(element.value == "No file chosen"){
            alert(helperMsg);
            elem.focus(); // set the focus to this input
            return false;
        }
        return true;
    }
    // If the length of the element's string is 0 then display helper message
    function notEmpty(elem, helperMsg){
        if(elem.value.length == 0){
            alert(helperMsg);
            elem.focus(); // set the focus to this input
            return false;
        }
        return true;
    }
    // If the element's string matches the regular expression it is all numbers
    function isNumeric(elem, helperMsg){
        var numericExpression = /^[0-9]+$/;
        if(elem.value.match(numericExpression)){
            return true;
        }else{
            alert(helperMsg);
            elem.focus();
            return false;
        }
    }
    function formValidator(){
        // Make quick references to our fields
        var carName = document.getElementById('name');
        var image = document.getElementById('image');
        var qty = document.getElementById('qty');
        var seat = document.getElementById('seat');
        var price = document.getElementById('price');

        // Check each input in the order that it appears in the form!
        if(notEmpty(carName, "Please enter car name")){
            if(checkimg(image, "Please choose images")){
                if(notEmpty(qty, "Please enter quantity")){
                    if(isNumeric(qty, "Quantity is number")){
                        if(notEmpty(seat, "Please enter seat quantity")){
                            if(isNumeric(seat,"seat quantity is number")){
                                if(notEmpty(price, "Please enter price")){
                                    if(isNumeric(price,"price is number")){

                                        return true;

                                    }

                                }
                            }
                        }
                    }
                }
            }
        }
        return false;
    }

    function loadXMLDoc(id,element)
    {
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById(id).innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET",element,true);
        xmlhttp.send();
    }
</script>